package cn.billy.org.controller;

import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.org.domain.Department;
import cn.billy.org.query.DepartmentQuery;
import cn.billy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Department getById(@ApiParam(value="部门主键",required = true)  @PathVariable("id") Long id){
        return iDepartmentService.getById(id);
    }

    @GetMapping
    public List<Department> loadAll(){
        return iDepartmentService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iDepartmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                iDepartmentService.save(department);

            } else {
                iDepartmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }

    @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = iDepartmentService.queryData(departmentQuery);
        return data;
    }

}
